// Test model factory implementation
// Copyright 2004 (C) Ralph Thomas

#include <model/test/factory.h>
#include <model/test/testModel.h>

namespace model {
	model* testFactory::fromString( util::string serialized ) const {
		url* theUrl = url::fromString( serialized );
		//
		// We need a valid URL, and one which is pointing at "test://"
		// to begin.
		//
		if( !theUrl ) return 0; 
		if( theUrl->getProtocol() != "test" ) {
			delete theUrl;
			return 0;
		}
		//
		// Create a test model, try to initialize it, return.
		//
		testModel* tm = new testModel( theUrl );
		delete theUrl;
		if( tm->init() ) return tm;
		tm->release();
		return NULL;
	}

	url* testFactory::normalize( const url* theUrl ) const {
		//
		// Really simple normalization -- we just remove excess "/"es
		// from the end of the URL.
		//
		if( !theUrl || (theUrl->getProtocol() != "test") ) return NULL;
		util::string path = theUrl->getPath();
		unsigned int i = path.rfind( "/" );
		while( (i != (unsigned int) -1) && !path.empty() ) {
			path = path.substr( 0, i );
			i = path.rfind( "/" );
		}
		url* outUrl = url::fromUrl( theUrl );
		outUrl->setPath( path );

		return outUrl;
	}
};

